package com.weishi.project.training.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.project.api.ret.SportsManAchievement;
import com.weishi.project.api.ret.TrainingAllDataInfoRet;
import com.weishi.project.api.ret.TrainingDataRet;
import com.weishi.project.api.vo.SportManBestResultVo;
import com.weishi.project.api.vo.TrainingAchievementMaxVo;
import com.weishi.project.api.vo.TrainingAchievementTopVo;
import com.weishi.project.api.vo.TrainingDataVo;
import com.weishi.project.sport.trainingdata.vo.WaterRankingVO;
import com.weishi.project.training.domain.*;
import com.weishi.project.training.mapper.TrainingAchievementMapper;
import com.weishi.project.training.mapper.TrainingDayMapper;
import com.weishi.project.training.service.ISportsmanService;
import com.weishi.project.training.service.ITrainingAchievementService;
import com.weishi.project.training.service.ITrainingGroupChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TrainingAchievementService业务层处理
 */
@Service
public class TrainingAchievementServiceImpl extends ServiceImpl<TrainingAchievementMapper, TrainingAchievement> implements ITrainingAchievementService {

    @Autowired
    private TrainingAchievementMapper trainingAchievementMapper;

    @Autowired
    private TrainingDayMapper trainingDayMapper;

    @Autowired
    private ITrainingGroupChannelService trainingGroupChannelService;

    @Autowired
    private ISportsmanService sportsmanService;

    /**
     * 查询TrainingAchievement列表
     */
    @Override
    public List<TrainingAchievement> selectTrainingAchievementList(TrainingAchievement trainingAchievement) {
        return trainingAchievementMapper.selectTrainingAchievementList(trainingAchievement);
    }

    @Override
    public List<AchievementExcel> selectTrainingAchievementListExcel(TrainingAchievement trainingAchievement) {
        return trainingAchievementMapper.selectTrainingAchievementListExcel(trainingAchievement);
    }

    @Override
    public List<TrainingAchievement> selectTrainingAchievementList1(TrainingAchievement trainingAchievement) {
        return trainingAchievementMapper.selectTrainingAchievementList1(trainingAchievement);
    }

    @Override
    public List<Map<String, Object>> qryBestResultSpecialProject(SportManBestResultVo sportManBestResultVo) {
        return trainingAchievementMapper.qryBestResultSpecialProject(sportManBestResultVo);
    }

    @Override
    public TrainingDataRet qryTrainingDataInfoByTrainingDay(int trainingDayId) {
        TrainingDataRet trainingDataRet = null;
        TrainingDay trainingDay =trainingDayMapper.selectById(trainingDayId);
        if(trainingDay!=null){
            trainingDataRet = TrainingDataRet.getInstan(trainingDay);
            List<SportsManAchievement> list = trainingAchievementMapper.qryTrainingAchievementByTrainingDayId(trainingDay.getId());
            trainingDataRet.setSportsMans(list);
        }
        return trainingDataRet;
    }

    @Override
    public List<Map<String, Object>> qryTrainingDataInfoBySportManId(Map<String, Object> params) {
        return trainingAchievementMapper.qryTrainingDataInfoBySportManId(params);
    }

    @Override
    public List<TrainingAchievementTopVo> qryTrainingAchievementTop(Map<String, Object> params) {

        List<TrainingGroupChannel> trainingGroupChannelList = trainingGroupChannelService.qryBestTop10(params,true);

        List<TrainingAchievementTopVo>  ret = new ArrayList<>();
        for(TrainingGroupChannel trainingGroupChannel : trainingGroupChannelList){
            TrainingAchievementTopVo vo = new TrainingAchievementTopVo();
            vo.setAchievement(trainingGroupChannel.getAchievement());
            vo.setChannelId(trainingGroupChannel.getId());
            vo.setBoatType(trainingGroupChannel.getBoatType());
            vo.setPropellerFrequency(trainingGroupChannel.getPropellerFrequency());
            List<TrainingAchievement> persons = trainingGroupChannel.getPerson();
            if(persons!=null && persons.size()>0){
                TrainingAchievement trainingAchievement = persons.get(0);
                String sportsmanName = sportsmanService.qrySportsmanByCache(trainingAchievement.getSportsmanId()).getName();
                if(persons.size()==1){
                    vo.setName(sportsmanName);
                    vo.setHeartRate(trainingAchievement.getHeartRate());
                    vo.setLacticAcid(trainingAchievement.getLacticAcid());
                    vo.setId(trainingAchievement.getId());
                }else {
                    vo.setId(trainingAchievement.getId());
                    vo.setName(sportsmanName+"...");
                }
            }
            ret.add(vo);
        }

        /*List<TrainingAchievementTopVo>  list = trainingAchievementMapper.qryTrainingAchievementTop(params);
        Map<Integer,List<TrainingAchievementTopVo>> map = new HashMap<>();
        for(TrainingAchievementTopVo vo : list){
            List<TrainingAchievementTopVo>  temp = map.get(vo.getChannelId());
            if(temp==null){
                temp = new ArrayList<>();
                map.put(vo.getChannelId(),temp);
            }
            temp.add(vo);
        }

        List<TrainingAchievementTopVo>  ret = new ArrayList<>();
        for(Integer key : map.keySet()){
            List<TrainingAchievementTopVo> temp = map.get(key);
            TrainingAchievementTopVo vo = temp.get(0);
            String name = "";
            int i = 0;
            for(TrainingAchievementTopVo topVo : temp){
                if(++i==2){
                    name += "...";
                    break;
                }
                name += topVo.getName() + " ";
            }
            vo.setName(name);
            ret.add(vo);
        }*/
        return ret;
    }


    @Override
    public List<TrainingAchievementMaxVo> selectTrainingAchievementListByChannelId(int channelId) {
        return trainingAchievementMapper.selectTrainingAchievementListByChannelId(channelId);
    }

    @Override
    public List<Map<String, Object>> selectClassHourBoatType(Integer sportsManId) {
        Map<String,Object> params = new HashMap<>();
        params.put("sportsManId",sportsManId);
        return trainingAchievementMapper.selectClassHourBoatType(params);
    }

    @Override
    public List<Map<String, Object>> selectKilometersBoatType(Map<String, Object> params) {
        return trainingAchievementMapper.selectKilometersBoatType(params);
    }

    @Override
    public List<TrainingAchievement> trainingAchievementList(Map<String, Object> params) {
        return trainingAchievementMapper.trainingAchievementList(params);
    }

    @Override
    public void updateTrainingAchievement(TrainingAchievement trainingAchievement) {
        trainingAchievementMapper.updateTrainingAchievement(trainingAchievement);
    }

    @Override
    public void deleteTrainingAchievement(TrainingAchievement trainingAchievement) {
        trainingAchievementMapper.deleteTrainingAchievement(trainingAchievement);
    }

    @Override
    public void saveTrainingAchievement(TrainingAchievement trainingAchievement) {
        trainingAchievementMapper.saveTrainingAchievement(trainingAchievement);
    }

    @Override
    public String selectSportsmanNameById(int sportsmanId) {
        return trainingAchievementMapper.selectSportsmanNameById(sportsmanId);
    }

    @Override
    public TrainingAllDataInfoRet qryTrainingAchievementDetailedReport(Integer trainingAchievementId, Integer type) {
        TrainingAllDataInfoRet ret = new TrainingAllDataInfoRet();
        Map<String,Object> params = new HashMap<> ();
        params.put("type",type==1?"special_training_water":"land_specific_training_dynamometer");
        params.put("trainingAchievementId",trainingAchievementId);
        Map<String,Object> retMap = trainingAchievementMapper.selectTrainingAchievementDetailedReport(params);
        if(retMap!=null){
            TrainingAllDataInfoRet.BaseInfo baseInfo = ret.getBaseInfo();
            if(retMap.get("boatType")!=null) baseInfo.setBoatType(retMap.get("boatType").toString());
            if(retMap.get("firstLevel")!=null) baseInfo.setFirstLevel(retMap.get("firstLevel").toString());
            if(retMap.get("secondLevel")!=null) baseInfo.setSecondLevel(retMap.get("secondLevel").toString());
            if(retMap.get("sportsmanId")!=null) baseInfo.setSportsmanName(sportsmanService.qrySportsmanByCache((Integer) retMap.get("sportsmanId")).getName());
            baseInfo.setSportsmanId(Integer.parseInt(retMap.get("sportsmanId").toString()));
            if(retMap.get("day")!=null) baseInfo.setTrainingDay(retMap.get("day").toString());
            if(retMap.get("groupName")!=null) baseInfo.setGroupName(retMap.get("groupName").toString());

            TrainingAllDataInfoRet.AchievementInfo achievementInfo = ret.getAchievementInfo();
            if(retMap.get("id")!=null) achievementInfo.setId(retMap.get("id").toString());
            if(retMap.get("achievement")!=null) achievementInfo.setAchievement(retMap.get("achievement").toString());
            if(retMap.get("propellerFrequency")!=null) achievementInfo.setPropellerFrequency(retMap.get("propellerFrequency").toString());
            if(retMap.get("watt")!=null)
            {
                achievementInfo.setWatt(retMap.get("watt").toString());
            }else{
                achievementInfo.setWatt(null);
            }
            if(retMap.get("power")!=null)
            {
                achievementInfo.setPower(retMap.get("power").toString());
            }else{
                achievementInfo.setPower(null);
            }
            if(retMap.get("rowingKilometers")!=null) achievementInfo.setRowingKilometers(retMap.get("rowingKilometers").toString());
            if(retMap.get("segmentedTiming")!=null) achievementInfo.setSegmentedTimings(retMap.get("segmentedTiming").toString());

            TrainingAllDataInfoRet.TrainingGroundInfo training = ret.getTrainingGroundInfo();
            if(retMap.get("airTemperature")!=null)
            {
                training.setAirTemperature(retMap.get("airTemperature").toString());
            } else{
                training.setAirTemperature(null);
            }
            if(retMap.get("siteFactors")!=null)
            {
                training.setSiteFactors(retMap.get("siteFactors").toString());
            }else{
                training.setSiteFactors(null);
            }

            if(retMap.get("weather")!=null)
            {
                training.setWeather(retMap.get("weather").toString());
            }else{
                training.setWeather(null);
            }
            if(retMap.get("windSpeed")!=null)
            {
                training.setWindSpeed(retMap.get("windSpeed").toString());
            }else{
                training.setWindSpeed(null);
            }
            List<Map<String,Object>>  sportsManHRs = new ArrayList<>();
            List<Map<String,Object>>  sportsManLAs = new ArrayList<>();
            TrainingAchievement trainingAchievement = new TrainingAchievement();
            if(retMap.get("channelId")!=null) trainingAchievement.setChannelId(Integer.parseInt(retMap.get("channelId").toString()));
            List<TrainingAchievement> trainingAchievements = trainingAchievementMapper.selectTrainingAchievementList(trainingAchievement);
            for(TrainingAchievement achievement : trainingAchievements){
                Map<String,Object> mapHr = new HashMap<>();
                Map<String,Object> mapLa = new HashMap<>();

                Sportsman sportsman = sportsmanService.qrySportsmanByCache(achievement.getSportsmanId());
                mapHr.put("sportsmanName",sportsman.getName());
                mapHr.put("sportsmanId" , sportsman.getId());
                mapHr.put("groupId" , achievement.getGroupId());
                mapHr.put("channelId" , achievement.getChannelId());
                mapHr.put("shipSpeedId" , achievement.getShipSpeedId());
                mapHr.put("trainingDayId" , achievement.getTrainingDayId());
                if(achievement.getPosition()!=null) mapHr.put("position",achievement.getPosition()==-1?"舵手":achievement.getPosition()+"号位");
                if(achievement.getHeartRate()!=null) {
                    mapHr.put("HRs",achievement.getHeartRate().split(","));
                }else{
                    mapHr.put("HRs","".split(","));
                }
                if(achievement.getId()!=null) mapHr.put("memo",achievement.getId());

                mapLa.put("sportsmanName",sportsman.getName());
                mapLa.put("sportsmanId" , sportsman.getId());
                mapLa.put("groupId" , achievement.getGroupId());
                mapLa.put("channelId" , achievement.getChannelId());
                mapLa.put("shipSpeedId" , achievement.getShipSpeedId());
                mapLa.put("trainingDayId" , achievement.getTrainingDayId());
                if(achievement.getPosition()!=null) mapLa.put("position",achievement.getPosition()==-1?"舵手":achievement.getPosition()+"号位");
                if(achievement.getLacticAcid()!=null) {
                    mapLa.put("LAs",achievement.getLacticAcid().split(","));
                }else{
                    mapLa.put("LAs","".split(","));
                }
                if(achievement.getId()!=null) mapLa.put("memo",achievement.getId());

                sportsManHRs.add(mapHr);
                sportsManLAs.add(mapLa);
            }
            ret.setSportsManHRs(sportsManHRs);
            ret.setSportsManLAs(sportsManLAs);
        }
        return ret;
    }

    @Override
    public List<Map<String, Object>> qryDetailedReportByParams(TrainingDataVo trainingDataVo) {
        List<Integer> sportsmanIds = trainingDataVo.getSportsmanIds();
        List<Integer> sportswomanIds = trainingDataVo.getSportswomanIds();

        if(sportswomanIds!=null && sportswomanIds.size()!=0 && sportswomanIds.get(0)==-1){
            //女 1
            sportswomanIds = this.sportsmanService.selectSportsmanIdListBySex(1);
        }
        if(sportsmanIds!=null && sportsmanIds.size()!=0 && sportsmanIds.get(0)==-1){
            //男 0
            sportsmanIds = this.sportsmanService.selectSportsmanIdListBySex(0);
        }

        if(sportsmanIds==null || sportsmanIds.size()==0){
            sportsmanIds = new ArrayList<>();
        }
        if(sportswomanIds!=null && sportswomanIds.size()>0){
            sportsmanIds.addAll(sportswomanIds);
        }
        trainingDataVo.setSportsmanIds(sportsmanIds);
        return trainingAchievementMapper.selectDetailedReportByParams(trainingDataVo);
    }

    @Override
    public boolean updateById(TrainingAchievement entity) {
        return super.updateById(entity);
    }



    /**
     * 查询运动员水上专项训练--最好成绩排名
     */
    @Override
    public List<WaterRankingVO> getWaterRankingVO(Integer userId , Integer trainingMode , Integer kilometers , Integer sex , Integer realClassHour) {
        return trainingAchievementMapper.getWaterRankingVO(userId , trainingMode , kilometers , sex , realClassHour);
    }


    /**
     * 查询运动员水上专项训练--根据 运动员名称查询排名
     */
    @Override
    public List<WaterRankingVO> getWaterRankingNameVO(String name , Integer trainingMode, Integer kilometers , Integer sex) {
        return trainingAchievementMapper.getWaterRankingNameVO(name , trainingMode , kilometers , sex);
    }


    /**
     * 查询运动员陆上专项训练--最好成绩排名
     */
    @Override
    public List<WaterRankingVO> getWaterRankingVOLU(Integer userId, Integer trainingMode, Integer kilometers , Integer sex , Integer realClassHour) {
        return trainingAchievementMapper.getWaterRankingVOLU(userId , trainingMode , kilometers , sex , realClassHour);
    }


    /**
     * 查询运动员专项训练--根据 运动员名称查询排名
     */
    @Override
    public List<WaterRankingVO> getWaterRankingNameVOLU(String name, Integer trainingMode, Integer kilometers , Integer sex) {
        return trainingAchievementMapper.getWaterRankingNameVOLU(name , trainingMode , kilometers , sex);
    }


    /**
     * 运动员 -- 今日专项成绩排名 第一次查询
     */
    @Override
    public List<WaterRankingVO> getThisDayRankingVO(Integer groupId  , String boatType , Integer trainingDayId, Integer sex, Integer trainingMode , Integer kilometers , Double realClassHour , String firstLevel) {
        return trainingAchievementMapper.getThisDayRankingVO(groupId , boatType , trainingDayId , sex , trainingMode , kilometers , realClassHour , firstLevel);
    }


    /**
     * 查询运动员陆上专项训练--根据 运动员名称查询排名 第二次查询
     */
    @Override
    public List<WaterRankingVO> getThisDayRankingVOSS(String firstLevel , String name, Integer trainingMode, Integer kilometers, Integer sex) {
        return trainingAchievementMapper.getThisDayRankingVOSS(firstLevel , name , trainingMode , kilometers , sex);
    }

    @Override
    public void deleteTrainingAchievementByChannelId(Integer channelId) {
        trainingAchievementMapper.deleteTrainingAchievementByChannelId(channelId);
    }
}